package com.xggstudio.crm.ui.mvp.p;
import android.support.annotation.NonNull;
import android.util.Log;

import com.xggstudio.crm.api.APIServer;
import com.xggstudio.crm.api.NetCheckerSubscriber;
import com.xggstudio.crm.base.App;
import com.xggstudio.crm.ui.mvp.m.BaseResult;
import com.xggstudio.crm.ui.mvp.m.CusInfo;
import com.xggstudio.crm.ui.mvp.v.ICusInfo;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class P_CusInfo {

    private ICusInfo iCusInfo;

    public P_CusInfo(ICusInfo iCusInfo) {
        this.iCusInfo = iCusInfo;
    }

    public void getInfo(int id){

        String token = App.getInstance().getLoginBean().getResult().getAccess_token();

        APIServer.getInstence().getServer()
                .getCusInfo(id,token)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new NetCheckerSubscriber<CusInfo>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(CusInfo cusInfo) {
                        if(cusInfo.getCode().equals("1")){
                            iCusInfo.showInfo(cusInfo);
                        }else{

                        }
                    }

                    @Override
                    protected void onNoNetError() {
                        iCusInfo.noNet();
                    }
                });
    }

    public void addCus(CusInfo info){

        String token = App.getInstance().getLoginBean().getResult().getAccess_token();

        Map<String,String> temp = new HashMap<>();

        int count = info.getResult().getAssist().size();


        temp.put("client_name",info.getResult().getUser().getClient_name());
        temp.put("client_documentary",info.getResult().getUser().getClient_documentary());
        temp.put("client_phone",info.getResult().getUser().getClient_phone());
        temp.put("client_qq",info.getResult().getUser().getClient_qq());
        temp.put("client_wechat",info.getResult().getUser().getClient_wechat());
        temp.put("client_sex",info.getResult().getUser().getClient_sex()+"");
        temp.put("client_online",info.getResult().getUser().getClient_online()+"");
        temp.put("client_age",info.getResult().getUser().getClient_age());
        temp.put("client_company",info.getResult().getUser().getClient_company());
        temp.put("client_province",info.getResult().getUser().getClient_province());
        temp.put("client_city",info.getResult().getUser().getClient_city());
        temp.put("client_county",info.getResult().getUser().getClient_county());
        temp.put("client_address",info.getResult().getUser().getClient_address());
        temp.put("class_id",info.getResult().getUser().getClass_id()+"");
        temp.put("client_follows",info.getResult().getUser().getClient_name());

        Set<Map.Entry<String, String>> set = temp.entrySet();

        for (Map.Entry<String, String> me : set){
            String key = me.getKey();
            String value = me.getValue();

            Log.e("P_CusInfo", "addCus: " + key + "---" + value);
        }



        if(count > 0) {

            int[] assist = new int[count];
            int[] percent = new int[count];
            for (int i = 0; i < count; i++) {
                CusInfo.ResultBean.AssistBean b = info.getResult().getAssist().get(i);
                assist[i] = b.getUser_id();
                percent[i] = b.getPercent();
            }

            //temp.put("assist[]", assist);
            //temp.put("percent[]",percent);
        }




       /* APIServer.getInstence().getServer()
                .addcusData(token,temp)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new NetCheckerSubscriber<BaseResult>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(BaseResult baseResult) {
                        if(baseResult.equals("1")){
                            iCusInfo.addsucceed();
                        }else{
                            iCusInfo.showError(baseResult.getMessage());
                        }
                    }

                    @Override
                    protected void onNoNetError() {
                            iCusInfo.noNet();
                    }
                });*/
    }

}
